package com.springcloud.order.modular.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springcloud.commom.api.R;
import com.springcloud.commom.api.ResultCode;
import com.springcloud.commom.exception.ServiceException;
import com.springcloud.commom.factory.PageFactory;
import com.springcloud.commom.page.PageResult;
import com.springcloud.order.entity.Order;
import com.springcloud.order.modular.order.mapper.OrderMapper;
import com.springcloud.order.modular.order.service.OrderService;
import com.springcloud.order.param.OrderParam;
import com.springcloud.stock.feign.IStoreClient;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * service接口实现类
 *
 * @author hzl
 * @date 2024-07-30 15:48:28
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private IStoreClient storeClient;

    @Override
    public PageResult<Order> page(OrderParam orderParam) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(orderParam)) {

            // 根据商品id 查询
            if (ObjectUtil.isNotEmpty(orderParam.getProductId())) {
                queryWrapper.lambda().eq(Order::getProductId, orderParam.getProductId());
            }
            // 根据订单数量 查询
            if (ObjectUtil.isNotEmpty(orderParam.getOrderNum())) {
                queryWrapper.lambda().eq(Order::getOrderNum, orderParam.getOrderNum());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<Order> list(OrderParam orderParam) {
        return this.list();
    }

    @Override
    public void add(OrderParam orderParam) {
        Order order = new Order();
        BeanUtil.copyProperties(orderParam, order);
        this.save(order);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(OrderParam orderParam) {
        this.removeById(orderParam.getId());
    }

    @Override
    @GlobalTransactional(rollbackFor = Exception.class) // 开启全局事务
    public void buyProduct(String pid, Integer num) {
        log.info("order-service begin,XID：{}", RootContext.getXID());
        Order order = new Order();

        order.setProductId(pid);
        order.setOrderNum(num);

        save(order);


        R<String> result = storeClient.decreaseStore(pid, num);
        if (result.getCode() != 200) {
            throw new ServiceException(result.getMsg());
        }

        int i = 1 / 0;
        log.info("结束调用!");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(OrderParam orderParam) {
        Order order = this.queryOrder(orderParam);
        BeanUtil.copyProperties(orderParam, order);
        this.updateById(order);
    }

    @Override
    public Order detail(OrderParam orderParam) {
        return this.queryOrder(orderParam);
    }

    /**
     * 获取
     *
     * @author hzl
     * @date 2024-07-30 15:48:28
     */
    private Order queryOrder(OrderParam orderParam) {
        Order order = this.getById(orderParam.getId());
        if (ObjectUtil.isNull(order)) {
            throw new ServiceException(ResultCode.INTERNAL_SERVER_ERROR);
        }
        return order;
    }
}
